package com.delivery.adapter;

import java.util.ArrayList;
import java.util.List;


import org.json.JSONException;
import org.json.JSONObject;

import com.delivery.activity.PicturesActivity;
import com.delivery.activity.R;
import com.delivery.activity.PicturesActivity.MyHandler;
import com.delivery.common.ShareWeibo;
import com.delivery.module.CategoriesContent;
import com.delivery.module.ConfigModule;
import com.delivery.utils.FileUtils;
import com.delivery.utils.GraphicUtils;
import com.delivery.utils.HttpDownloader;
import com.delivery.utils.StringUtils;

import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;


public class PicturesAdapter extends BaseAdapter {
	private List<CategoriesContent> mList; //一次显示的记录条数
	private Context mContext;
	public static  int APP_PAGE_SIZE = 1;
	private PackageManager pm;
	public MyHandler myHandler;
	public PicturesAdapter(Context context, List<CategoriesContent> list, int page) {
		mContext = context;
		pm = context.getPackageManager();
		
		if(mList!=null && mList.size() > 0){
			mList.clear();
		}
		
		mList = new ArrayList<CategoriesContent>();
		int i = page * APP_PAGE_SIZE;
		int iEnd = i+APP_PAGE_SIZE;
		while ((i<list.size()) && (i<iEnd)) {
			mList.add(list.get(i));
			i++;
		}
	}
	
	public int getCount() {
		return mList.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mList.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		CategoriesContent categoriesContent = mList.get(position);
		AppItem appItem;
		if (convertView == null) {
			View v = LayoutInflater.from(mContext).inflate(R.layout.mryt_info, null);
			
			appItem = new AppItem();
			appItem.mAppIcon = (ImageView)v.findViewById(R.id.imgdetail);
			appItem.imageTime = (TextView) v.findViewById(R.id.image_time);
			appItem.contentDesc = (TextView) v.findViewById(R.id.contentDesc);
			appItem.btShare = (Button) v.findViewById(R.id.share_address);
			v.setTag(appItem);
			//监听小类事件
			convertView = v;
		} else {
			appItem = (AppItem)convertView.getTag();
		}
		
		if(categoriesContent == null){
			return convertView;
		}
		HttpDownloader httpDownloader = new HttpDownloader();
		if(!FileUtils.isExistFile(categoriesContent.getLocalFileUrl())){
			int result = httpDownloader.downFile(ConfigModule.GET_Context_URL +categoriesContent.getImgUrl(),"/upload/",StringUtils.subStrIndexOf(categoriesContent.getImgUrl(),"/"));
			//url = ConfigModule.GET_Context_URL + categoriesContent.getImgUrl();
		//}else{
			//url = "/upload/"+StringUtils.subStrIndexOf(categoriesContent.getImgUrl(),"/");
		}
		
		Bitmap bit = GraphicUtils.CreateBitmapFromLocal(categoriesContent.getLocalFileUrl());
		appItem.mAppIcon.setImageBitmap(bit);
		appItem.contentDesc.setText(categoriesContent.getContentDesc());
		appItem.imageTime.setText("作品日期："+StringUtils.subStrLen(categoriesContent.getUpdateTime(),10));
		appItem.btShare.setOnClickListener(new Share(ConfigModule.GET_Context_URL + categoriesContent.getImgUrl()));
		return convertView;
	}
	
	class Share implements OnClickListener{
		
		private String imgUrl;
		
		public Share(String imgUrl){
			this.imgUrl = imgUrl;
		}
		
		public void onClick(View v) {
			new Thread(){
				public void run(){
					String result = ShareWeibo.shareContent(imgUrl);
					try {
						if(StringUtils.isEmpty(result)){
							return;
						}
						JSONObject jsonObject = new JSONObject(result);
						if(jsonObject!=null && StringUtils.isNotEmpty(jsonObject.getString("type"))){
							if(jsonObject.getString("type").equals("success")){
								StringUtils.ToskShow(mContext, "您成功分享图片到新浪微博。");
							}else{
								StringUtils.ToskShow(mContext, "分享失败!");
							}
						}
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}
			};
		}
		
	}
	/**
	 * 每个应用显示的内容，包括图标和名称
	 * @author Yao.GUET
	 *
	 */
	class AppItem {
		ImageView mAppIcon;
		TextView mAppName;
		TextView imageTime;
		TextView contentDesc;
		Button btShare;
	}
}
